package com.zjeav.lingjiao.ui.book;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.response.BookDetailResponse;
import com.zjeav.lingjiao.base.service.BookDetailService;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class BookDetailModel {
    public void getBookDetail(int id, final OnGetBookDetailListener listener) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BookDetailService service = retrofit.create(BookDetailService.class);
        service.getBookDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<BookDetailResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result<BookDetailResponse> bookDetailResponseResult) {
                        listener.OnGetDetailSuccess(bookDetailResponseResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.OnGetDetailFail(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public interface OnGetBookDetailListener {
        void OnGetDetailSuccess(BookDetailResponse detailResponse);

        void OnGetDetailFail(Throwable e);
    }
}
